<?php

abstract class Common_Chart_Strategy_Google_Bar extends Common_Chart_Strategy_Google_AxisChart {

    const RELATIVE_BAR_SIZE = 'r';

    public function __construct() {
        parent::__construct();
    }

    protected function get_colour_delimiter() {
        return ',';
    }

    protected function apply_render_options($arr_chart_parameters, $arr_data, $arr_headers, $arr_options = array()) {

        $arr_chart_parameters = parent::apply_render_options($arr_chart_parameters, $arr_data, $arr_headers, $arr_options);
        $arr_chart_parameters = $this->apply_bar_options($arr_chart_parameters, $arr_data, $arr_options);

        return $arr_chart_parameters;
    }

    protected function apply_bar_options($arr_chart_parameters, $arr_data, $arr_options = array()) {

        $str_bar_size = self::RELATIVE_BAR_SIZE; // relative
        if(isset($arr_options['bar_size']) && is_numeric($arr_options['bar_size'])) {
            $str_bar_size = $arr_options['bar_size'];
        }

        $flt_bar_spacing = 0.5;
        if(isset($arr_options['spacing_between_bars']) && is_numeric($arr_options['spacing_between_bars'])) {
            $flt_bar_spacing = $arr_options['spacing_between_bars'];
        }

        $flt_group_spacing = 2;
        if(isset($arr_options['spacing_between_groups']) && is_numeric($arr_options['spacing_between_groups'])) {
            $flt_group_spacing = $arr_options['spacing_between_groups'];
        }

        $str_bar_spacing = $str_bar_size . ',' . $flt_bar_spacing . ',' . $flt_group_spacing;

        $arr_chart_parameters[self::PARAMETER_BAR_SPACING] = $str_bar_spacing;

        if(isset($arr_options['bar_gradients'])) {

            $arr_bar_gradients = (array) $arr_options['bar_gradients'];
            $arr_series = array_keys($arr_data);

            $i = 0;
            foreach($arr_series as $str_series_key) {
                if(isset($arr_bar_gradients[$str_series_key])) {

                    $arr_gradient_colours = explode(',', $arr_bar_gradients[$str_series_key]);
                    $int_colours_in_gradient = count($arr_gradient_colours);
                    if($int_colours_in_gradient > 1) {

                        $int_colour = 0;
                        $arr_colours = array();
                        foreach($arr_gradient_colours as $str_colour) {
                            $arr_colours[] = $str_colour . ',' . round(($int_colour / ($int_colours_in_gradient-1)), 2);
                            $int_colour++;
                        }

                        $str_gradient = 'b' . $i .',lg,90,' . implode(',', $arr_colours);

                        $arr_chart_parameters = $this->add_chart_fill($arr_chart_parameters, $str_gradient);

                    }

                }
                $i++;
            }

        }

        return $arr_chart_parameters;
    }

}
